package qt

import (
	"encoding/json"
	"fmt"
	"iot-base/auth/common"
	"iot-base/auth/db"
	types2 "iot-base/auth/types"
	"iot-base/common/ctrltype"
	"time"
)

func Move(param []byte) (interface{}, int) {
	var req types2.ReqContinuousMove
	json.Unmarshal(param, &req)
	profileToken, _ := getProfileToken(req.ThingId)
	if profileToken == "" {
		return nil, types2.ParamsError
	}
	req.ProfileToken = profileToken

	params := make(map[string]interface{})
	if req.Speed <= 0 || req.Speed > 10 {
		req.Speed = 5
	}
	// horizontal 调整左右旋转
	if req.X == "left" {
		params["X"] = -float64(req.Speed) / 10.0
	} else if req.X == "right" {

		params["X"] = float64(req.Speed) / 10.0
	}
	// vertical
	if req.Y == "up" {
		params["Y"] = float64(1) //float64(req.Speed) / 10.0
	} else if req.Y == "down" {
		params["Y"] = float64(-1) //-float64(req.Speed) / 10.0
	}

	// todo QT里面的Y,写成 X, 没了arm环境了，没法编译QT了，兼容错误
	if req.X == "up" {
		params["Y"] = float64(1) //float64(req.Speed) / 10.0
	} else if req.X == "down" {
		params["Y"] = float64(-1) //-float64(req.Speed) / 10.0
	}
	fmt.Println("ProfileToken", req.ProfileToken)
	// zoom
	params["ProfileToken"] = req.ProfileToken
	var cmd ctrltype.CtrlCmd
	// 移动焦距
	if req.Z != "" {
		cmd.ID = types2.RelativeZoomMove
		var zoom float64
		if req.Z == "big" {
			// 焦距放大
			zoom = float64(0.1)
		} else {
			// 焦距放小
			zoom = float64(-0.1)
		}
		cmd.Params = map[string]interface{}{
			"ProfileToken": req.ProfileToken,
			"Z":            zoom, // 焦距
			"Speed":        float64(req.Speed) / 10.0,
		}
	} else {
		cmd.ID = types2.ContinuousMove
		cmd.Params = params
	}
	cmd.ThingID = req.ThingId

	result, err := common.CtrlResp(&cmd)
	if result != nil && err == nil {
		return nil, types2.Success
	} else {
		return nil, types2.ExceptionError
	}

}

func getProfileToken(thingId string) (string, error) {
	key := "ProfileToken:" + thingId
	profileToken, _ := db.NewRedis().Get(key)
	if profileToken == "" {
		var cmd ctrltype.CtrlCmd
		cmd.ID = types2.GetProfiles
		cmd.ThingID = thingId
		result, err := common.CtrlResp(&cmd)
		if result != nil && err == nil {
			list := result["list"].([]interface{})
			listMap := list[0].(map[string]interface{})
			profileToken = listMap["ProfileToken"].(string)
			db.NewRedis().Set(key, profileToken, time.Hour*24)
			return profileToken, nil
		} else {
			return "", err
		}
	}

	return profileToken, nil
}

func Stop(data []byte) (interface{}, int) {
	var req types2.ReqContinuousMove
	if err := json.Unmarshal(data, &req); err != nil {
		return "", types2.ParamsError
	}
	profileToken, _ := getProfileToken(req.ThingId)
	if profileToken == "" {
		return nil, types2.ParamsError
	}
	req.ProfileToken = profileToken
	params := make(map[string]interface{})
	if req.Speed <= 0 || req.Speed > 10 {
		req.Speed = 5
	}
	// horizontal
	params["X"] = true
	// vertical
	params["Y"] = true
	params["ProfileToken"] = req.ProfileToken
	var cmd ctrltype.CtrlCmd
	cmd.ID = types2.StopMove
	cmd.ThingID = req.ThingId
	cmd.Params = params
	result, err := common.CtrlResp(&cmd)
	if result != nil && err == nil {
		return nil, types2.Success
	} else {
		return nil, types2.ExceptionError
	}
}
